//
//  ZOnceSubmitViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/24.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZOnceSubmitViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "JTTakeAlbumOrCamera.h"
#import "ZLabelTools.h"
#import "ZDoctorView.h"
#import "LZSectedPicVC.h"
#import "PointOutView.h"
#import "ZPartyView.h"
#import "JTAlertView.h"


#import "ZPayViewController.h"// 支付
#import "ZDoctorViewController.h" // 医生主页

#import <SDWebImage/UIButton+WebCache.h>
#import "SPVoiceManager.h"
static NSInteger  const oButtonTag = 200; // 按钮的tag值
@interface ZOnceSubmitViewController ()<ZJQPointOutDelegate>{
    ZPartyView * _consumerView; // 用户一栏
    // 病情描述
    UILabel * _describeLabel;
    UITextView * _describeTextView;
    //听语音
    UILabel * _voicelable;
    UIButton * _voiceBtn;
    // 图片部分
    UILabel * _addLabel; // 请你补充资料label
    UILabel * _consultLabel;
    UIButton * _imageBtn1;
    UIButton * _imageBtn2;
    UIButton * _imageBtn3;
    UILabel * _imageLabel1;
    UILabel * _imageLabel2;
    UILabel * _imageLabel3;
    
    ZDoctorView * _doctorView;// 医生一栏
    
    // 医生答复
    UILabel * _doctordescribeLabel;
    UITextView * _doctordescribeTextView;
    
    //听语音
    UILabel * _doctorvoicelable;
    UIButton * _doctorvoiceBtn;
    //跳转支付界面
    UIButton * _payBtn;
    // 再次提交的按钮
    UIButton * _againBtn;
//    NSMutableDictionary * _imageDict;// 存图片的dict
    NSUInteger  _selectIndex ;// 记录被选中的是哪一个按钮;
    // 两个时长的label
    UILabel * _voiceTime;
    UILabel * _d_voiceTime;
    
}

@property (nonatomic,strong) UIScrollView * mainScrollView;
@property (nonatomic,strong) PointOutView * pointOutView;

@property (nonatomic,strong) ZDidRDetailModel * didOModel;
@property (nonatomic,strong) ZDoctorTModel * dOModel;
@property (nonatomic,strong) LZUserInfo * userInfo;
@property (nonatomic,strong) AVAudioPlayer *avPlay;
@property (nonatomic,strong)NSMutableDictionary * imageDict;
@property (nonatomic,strong)NSMutableDictionary * imageUrlDict;

@property (nonatomic,strong) UIToolbar * bar;
@property (nonatomic,strong) UIToolbar * bar2;
@property (nonatomic,strong) UIToolbar * bar3;
@end

@implementation ZOnceSubmitViewController

// 移除通知中心
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if ([[SPVoiceManager defaultManager] isPlaying]) {
        [[SPVoiceManager defaultManager] stop];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"医生回复";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initialzeDatas];
    [self initlizeInterface];
    // 选中照片传回来的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(returnPicNoc:) name:@"returnMaxPic" object:nil];
}
// 返回选择的照片
- (void)returnPicNoc:(NSNotification *)sender {
    LZLog(@"----ss--%@",sender);
    NSDictionary *dic = sender.userInfo;
    if ([sender.userInfo[@"selecIndex"] integerValue]== 1) {
        [_imageBtn1 sd_setBackgroundImageWithURL:[NSURL URLWithString:dic[@"img_max"]] forState:UIControlStateNormal];
        
        // 拿到照片1
        NSString *tempStr = dic[@"img_max"];
        NSArray * array = [tempStr componentsSeparatedByString:IconURL];
        [_imageUrlDict setObject:[array lastObject] forKey:@"part"];
        [_imageDict removeObjectForKey:@"part"];
        
    }else if ([sender.userInfo[@"selecIndex"] integerValue] == 2) {
        [_imageBtn2 sd_setBackgroundImageWithURL:[NSURL URLWithString:dic[@"img_max"]] forState:UIControlStateNormal];
        // 拿到照片2
        NSString *tempStr = dic[@"img_max"];
        NSArray * array = [tempStr componentsSeparatedByString:IconURL];
        [_imageUrlDict setObject:[array lastObject] forKey:@"report"];
        [_imageDict removeObjectForKey:@"report"];
        
    }else {
        [_imageBtn3 sd_setBackgroundImageWithURL:[NSURL URLWithString:dic[@"img_max"]] forState:UIControlStateNormal];
        // 拿到照片3
        NSString *tempStr = dic[@"img_max"];
        NSArray * array = [tempStr componentsSeparatedByString:IconURL];
        [_imageUrlDict setObject:[array lastObject] forKey:@"xray"];
        [_imageDict removeObjectForKey:@"xray"];
        
    }
}

- (void)initialzeDatas {
    _imageDict = [@{} mutableCopy];
    _imageUrlDict = [@{} mutableCopy];
    _userInfo = [LZUserInfo getUserInfo];
    

}
- (void)initlizeInterface {
    //分享按钮
    UIButton * rightBtn = [[UIButton alloc] init];
    rightBtn.frame = CGRectMake(0, 0, 16, 13);
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"btn_share"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * rightBitem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    self.navigationItem.rightBarButtonItem = rightBitem;
    [self makeAllSubView];
    [self addALLSubView];
    [self makeAllSubviewMasnoy];
    [self makeGrayView];
}
- (void)makeAllSubView {
    __weak typeof(self) weakSelf = self;
    _consumerView = [[ZPartyView alloc] init];// 用户
    _describeLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_describeLabel Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:16 context:@"病情描述" isCenter:NO];
    _describeTextView = [[UITextView alloc] init];
    _describeTextView.backgroundColor = KHColorFromRGB(0xf4f1ce);
    _describeTextView.editable = YES;
    
    _voicelable = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_voicelable Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:16 context:@"用户语音" isCenter:NO];
    
    _voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_voiceBtn setBackgroundImage:[UIImage imageNamed:@"user_voice"] forState:UIControlStateNormal];
    [_voiceBtn addTarget:self action:@selector(oncePalyVoice:) forControlEvents:UIControlEventTouchUpInside];
    
    _consultLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_consultLabel Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:16 context:@"医生参考图" isCenter:NO];
    
    _addLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_addLabel Textcolor:[UIColor redColor] backColor:[UIColor clearColor] Fontsize:14 context:@"" isCenter:NO];
    
    _imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    _imageBtn1.tag = oButtonTag + 1;
    [_imageBtn1 setBackgroundImage:[UIImage imageNamed:@"Reference_picture"] forState:UIControlStateNormal];
    [_imageBtn1 addTarget:self action:@selector(addSubPointOutView:) forControlEvents:UIControlEventTouchUpInside];
    _imageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _imageBtn2.tag = oButtonTag + 2;
    [_imageBtn2 addTarget:self action:@selector(addSubPointOutView:) forControlEvents:UIControlEventTouchUpInside];
    [_imageBtn2 setBackgroundImage:[UIImage imageNamed:@"Reference_picture"] forState:UIControlStateNormal];
    _imageBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    _imageBtn3.tag = oButtonTag + 3;
    [_imageBtn3 addTarget:self action:@selector(addSubPointOutView:) forControlEvents:UIControlEventTouchUpInside];
    [_imageBtn3 setBackgroundImage:[UIImage imageNamed:@"Reference_picture"] forState:UIControlStateNormal];
    _imageLabel1 = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_imageLabel1 Textcolor:KHGaryColor backColor:[UIColor whiteColor] Fontsize:14 context:@"发病部位" isCenter:YES];
    _imageLabel2 = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_imageLabel2 Textcolor:KHGaryColor backColor:[UIColor whiteColor] Fontsize:14 context:@"病例报告" isCenter:YES];
    _imageLabel3 = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_imageLabel3 Textcolor:KHGaryColor backColor:[UIColor whiteColor] Fontsize:14 context:@"X光片" isCenter:YES];
    //医生部分
    _doctorView = [[ZDoctorView alloc] init];
    _doctorView.GoDoctorVCBlock = ^() {
        [weakSelf GoDotorDetail];
    };
    
    _doctordescribeLabel = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_doctordescribeLabel Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:16 context:@"医生回复" isCenter:NO];
    
    _doctordescribeTextView = [[UITextView alloc] init];
    _doctordescribeTextView.backgroundColor = KHGreenColor;
    _doctordescribeTextView .textColor = [UIColor whiteColor];
    _describeTextView.editable = NO;
    _doctordescribeTextView.editable = NO;
    _doctordescribeTextView.textColor = [UIColor whiteColor];
    
    _doctorvoicelable = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_doctorvoicelable Textcolor:[UIColor blackColor] backColor:[UIColor whiteColor] Fontsize:16 context:@"医生语音" isCenter:NO];
    
    _doctorvoiceBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [_doctorvoiceBtn setBackgroundImage:[UIImage imageNamed:@"doc_voice"] forState:UIControlStateNormal];
    [_doctorvoiceBtn addTarget:self action:@selector(onecePalyDotorVoice:) forControlEvents:UIControlEventTouchUpInside];
    

    _againBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _againBtn.backgroundColor = [UIColor whiteColor];
    _againBtn.backgroundColor = KHGreenColor;
    [_againBtn setTitle:@"再次提交" forState:UIControlStateNormal];
    [_againBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_againBtn addTarget:self action:@selector(againPostNetWorking) forControlEvents:UIControlEventTouchUpInside];
    _voiceTime = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_voiceTime Textcolor:[UIColor blackColor] backColor:[UIColor clearColor] Fontsize:14 context:@"" isCenter:NO];
    _voiceTime.textAlignment = NSTextAlignmentRight;
    _d_voiceTime = [[UILabel alloc] init];
    [ZLabelTools setThelabel:_d_voiceTime Textcolor:[UIColor blackColor] backColor:[UIColor clearColor] Fontsize:14 context:@"" isCenter:NO];
    _d_voiceTime.textAlignment = NSTextAlignmentRight;
    _doctorvoiceBtn.hidden = NO;
    _voiceBtn.hidden = NO;
    
}
- (void)addALLSubView {
    
    [self.view addSubview:self.mainScrollView];
    [self.view addSubview:_payBtn];
    [self.view addSubview:_againBtn];
    [self.mainScrollView addSubview:_describeLabel];
    [self.mainScrollView addSubview:_consumerView];
    [self.mainScrollView addSubview:_describeTextView];
    [self.mainScrollView addSubview:_voicelable];
    [self.mainScrollView addSubview:_voiceBtn];
    [self.mainScrollView addSubview:_consultLabel];
    [self.mainScrollView addSubview:_addLabel];
    [self.mainScrollView addSubview:_imageBtn1];
    [self.mainScrollView addSubview:_imageBtn2];
    [self.mainScrollView addSubview:_imageBtn3];
    [self.mainScrollView addSubview:_imageLabel1];
    [self.mainScrollView addSubview:_imageLabel2];
    [self.mainScrollView addSubview:_imageLabel3];
    
    [self.mainScrollView addSubview:_doctorView];
    [self.mainScrollView addSubview:_doctordescribeLabel];
    [self.mainScrollView addSubview:_doctordescribeTextView];
    [self.mainScrollView addSubview:_doctorvoicelable];
    [self.mainScrollView addSubview:_doctorvoiceBtn];
    [_voiceBtn addSubview:_voiceTime];
    [_doctorvoiceBtn addSubview:_d_voiceTime];
    
}
- (void)makeAllSubviewMasnoy {
    [_consumerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.mainScrollView);
        make.top.equalTo(self.mainScrollView).offset(10*ysIZE);
        make.height.offset(50*ysIZE);
    }];
    [_describeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_consumerView.mas_bottom).offset(16*ysIZE);
        make.width.equalTo(self.mainScrollView).multipliedBy(0.5);
        make.left.equalTo(self.mainScrollView).offset(10*xsIZE);
        make.height.mas_offset(16*ysIZE);
    }];
    [_describeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_describeLabel.mas_bottom).offset(15*ysIZE);
        make.left.equalTo(self.mainScrollView).offset(10*xsIZE);
        make.width.offset(300*xsIZE);
        make.height.offset(160*ysIZE);
    }];
    [_voicelable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_describeTextView.mas_bottom).offset(20*ysIZE);
        make.left.width.height.equalTo(_describeLabel);
    }];
    [_voiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_voicelable.mas_bottom).offset(14*ysIZE);
        make.left.equalTo(_voicelable);
        make.height.offset(40*ysIZE);
        make.width.offset(205*xsIZE);
    }];
    [_consultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_voiceBtn.mas_bottom).offset(35*ysIZE);
        make.left.height.equalTo(_describeLabel);
        make.width.offset(100*xsIZE);
    }];
    
    [_addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_consultLabel.mas_right).offset(5*xsIZE);
        make.height.top.equalTo(_consultLabel);
        make.width.offset(120*xsIZE);
    }];
    
    [_imageBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_consultLabel.mas_bottom).offset(15*ysIZE);
        make.left.equalTo(self.view).offset(25*xsIZE);
        make.width.offset(80*xsIZE);
        make.height.offset(80*xsIZE);
    }];
    [_imageBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_imageBtn1);
        make.left.equalTo(_imageBtn1.mas_right).offset(16*xsIZE);
    }];
    [_imageBtn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_imageBtn2);
        make.right.equalTo(self.view.mas_right).offset(-20*xsIZE);
    }];
    [_imageLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_imageBtn1.mas_bottom).offset(15*ysIZE);
        make.centerX.equalTo(_imageBtn1);
        make.height.offset(14*xsIZE);
        make.width.equalTo(_imageBtn1).multipliedBy(0.8);
    }];
    [_imageLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_imageLabel1);
        make.centerX.equalTo(_imageBtn2);
    }];
    [_imageLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.height.equalTo(_imageLabel2);
        make.centerX.equalTo(_imageBtn3);
    }];
    
    [_doctorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(self.mainScrollView);
        make.top.equalTo(_imageLabel1.mas_bottom).offset(35*ysIZE);
        make.height.offset(65*ysIZE);
    }];
    [_doctordescribeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.height.width.equalTo(_describeLabel);
        make.top.equalTo(_doctorView.mas_bottom).offset(15*ysIZE);
    }];
    [_doctordescribeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.equalTo(_describeTextView);
        make.height.offset(125*ysIZE);
        make.top.equalTo(_doctordescribeLabel.mas_bottom).offset(10*ysIZE);
    }];
    [_doctorvoicelable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(_voicelable);
        make.top.equalTo(_doctordescribeTextView.mas_bottom).offset(25*ysIZE);
    }];
    [_doctorvoiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(_voiceBtn);
        make.top.equalTo(_doctorvoicelable.mas_bottom).offset(14*ysIZE);
    }];
    
    [_againBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.bottom.equalTo(self.view);
        make.height.offset(50*ysIZE);
        make.width.equalTo(self.view);
    }];
    [_voiceTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.equalTo(_voiceBtn);
        make.right.equalTo(_voiceBtn).offset(-15*xsIZE);
        make.width.equalTo(_voiceBtn).multipliedBy(0.5);
    }];
    [_d_voiceTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.centerY.equalTo(_doctorvoiceBtn);
        make.right.equalTo(_doctorvoiceBtn).offset(-15*xsIZE);
        make.width.equalTo(_doctorvoiceBtn).multipliedBy(0.5);
    }];
}
- (void)makeGrayView {
    UIView * garyView1 = [[UIView alloc] init];
    garyView1.backgroundColor = MYGRAYCOLOR;
    UIView * garyView2 = [[UIView alloc] init];
    garyView2.backgroundColor = MYGRAYCOLOR;
    UIView * garyView3 = [[UIView alloc] init];
    garyView3.backgroundColor = MYGRAYCOLOR;
    [self.mainScrollView addSubview:garyView1];
    [self.mainScrollView addSubview:garyView2];
    [self.mainScrollView addSubview:garyView3];
    [garyView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.width.left.equalTo(self.mainScrollView);
        make.height.offset(10*ysIZE);
    }];
    [garyView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_consumerView.mas_bottom);
        make.width.equalTo(_consumerView).multipliedBy(0.8);
        make.height.offset(1.5*ysIZE);
        make.centerX.equalTo(_consumerView);
    }];
    [garyView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_doctorView.mas_top);
        make.width.equalTo(_doctorView);
        make.height.offset(10*ysIZE);
        make.centerX.equalTo(_doctorView);
    }];
}

// 三个图片按钮的方法
- (void)addSubPointOutView:(UIButton *)sender {
    
    _selectIndex = sender.tag - oButtonTag; // 记录要选照片的button
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self.pointOutView];
    
}
// 改变照片的方法
- (void)imagePickWithIndex:(NSInteger)cellIndex image:(UIImage *)image {
    if (cellIndex == 1) {
        [_imageBtn1 setImage:image forState:UIControlStateNormal];
        [self imageDictPutUpWithString:@"part" WithImage:image];
        return;
    }else if (cellIndex == 2) {
        [_imageBtn2 setImage:image forState:UIControlStateNormal];
        [self imageDictPutUpWithString:@"report" WithImage:image];
        return;
    }else if (cellIndex == 3) {
        [_imageBtn3 setImage:image forState:UIControlStateNormal];
        [self imageDictPutUpWithString:@"xray" WithImage:image];
        return;
    }
}

//  选择照片来源
- (void)SelectCellIndexPath:(NSInteger)cellSection index:(NSInteger)cellIndex {
    [self.pointOutView removeFromSuperview];
    if (cellSection == 1) {
        [self.pointOutView removeFromSuperview];
        return;
    }else {
        NSLog(@"%ld",(long)cellIndex);
        if (cellIndex == 0) {
           /// aaaaa
            LZSectedPicVC *selectedPic = [[LZSectedPicVC alloc] init];
            selectedPic.selectIndex = _selectIndex;
            [self.navigationController pushViewController:selectedPic animated:YES];
            return ;
        }else if (cellIndex == 1) {
            [JTTakeAlbumOrCamera takeAlbumWithViewcontroller:self withID:self];
            return;
        }else {
            [JTTakeAlbumOrCamera takeCameraWithViewcontroller:self withID:self];
            return;
        }
    }
}


// 选中照片后的回调;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self imagePickWithIndex:_selectIndex image:image];
    
}
- (void)imageDictPutUpWithString:(NSString *)imageName WithImage:(UIImage *)pImage {
    [_imageDict setObject:pImage forKey:imageName];
    [_imageUrlDict removeObjectForKey:imageName];
}
//点击播放录音
- (void)oncePalyVoice:(UIButton *)sender {
    if (!_didOModel || !_didOModel.voice || [_didOModel.voice isEqualToString:@"" ] || [_didOModel.voice isEqualToString:IconURL] ) {
        [YJProgressHUD showMessage:@"用户未录音" inView:self.view];
        return;
    }
    NSURL * voiceUrl = [[NSURL alloc]initWithString:_didOModel.voice];
    NSData * audioData = [NSData dataWithContentsOfURL:voiceUrl];
    NSString *name = [NSString stringWithFormat:@"/data.aac"];
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];
    [audioData writeToFile:filePath atomically:YES];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    
    [[SPVoiceManager defaultManager]playingMusicUrl:fileURL];
}
- (void)onecePalyDotorVoice:(UIButton *)sender {
    if (!_didOModel || !_didOModel.d_voice || [_didOModel.voice isEqualToString:@""]|| [_didOModel.voice isEqualToString:IconURL] ) {
        [YJProgressHUD showMessage:@"医生未录音" inView:self.view];
        return;
    }
    NSURL * voiceUrl = [[NSURL alloc]initWithString:_didOModel.d_voice];
    NSData * audioData = [NSData dataWithContentsOfURL:voiceUrl];
    NSString *name = [NSString stringWithFormat:@"/doctorData.aac"];
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:name];
    [audioData writeToFile:filePath atomically:YES];
    NSURL *fileURL = [NSURL fileURLWithPath:filePath];
    
    [[SPVoiceManager defaultManager]playingMusicUrl:fileURL];
}

// 重新发布问题给医生
- (void)againPostNetWorking {
    NSMutableDictionary * upDict = [@{@"pid":self.qId} mutableCopy];
    if (_imageDict.allKeys.count == 0 && _imageUrlDict.allKeys.count) {
        [YJProgressHUD showMessage:@"请选择照片" inView:self.view];
        return;
    }
    for (NSString * key in _imageUrlDict.allKeys) {
        [_imageUrlDict setObject:_imageUrlDict[key] forKey:key];
    }
    
    [BaseRequest zRequestWithFile:TestURL paramars:upDict paramarsSite:@"Index/againImage" fileDict:_imageDict progressBlock:^(NSProgress *uploadProgress) {
        
    } successBlock:^(id content) {
        NSMutableDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] == 1) {
            [YJProgressHUD showMessage:@"重新发布成功" inView:self.view];
            [self upImageSuccess];
        }else {
            [YJProgressHUD showMessage:@"上传失败" inView:self.view];
        }
    } failure:^(NSError * error) {
        [YJProgressHUD showError:@"上传失败，请重试" inview:self.view];
    }];
}

- (void)upImageSuccess {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        if (self.onceDelegate && [self.onceDelegate conformsToProtocol:@protocol(OnceSubDelegate)]) {
            [self.onceDelegate refurbish];
        }
        [self.navigationController popViewControllerAnimated:YES];
    });
    
}

- (void)setOPersonModel:(PersonInfoModel *)oPersonModel {
    _oPersonModel = oPersonModel;
    _consumerView.perModel = _oPersonModel;
}

- (void)setQId:(NSString *)qId {
    _qId = qId;
    [self answerNetWorking];
}
// 加载数据的网络请求
- (void)answerNetWorking {
    NSDictionary * dict = @{@"pid":self.qId};
    [YJProgressHUD showProgress:@"正在加载" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Index/answerDetails" sucessBlock:^(id content) {
        NSDictionary * rdict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        LZLog(@"yishenhuifu%@",rdict);
        if ([rdict[@"code"] isEqualToString:@"1"]) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            [self hanldReplyData:rdict[@"list"] ];
        }else {
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
        
        
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];

}
- (void)hanldReplyData:(NSDictionary * )dict {
    self.didOModel = [[ZDidRDetailModel alloc] initWithDict:[dict mutableCopy]];
    self.dOModel = [[ZDoctorTModel alloc] initWithDict:[dict mutableCopy]];
    
}
- (void)setDidOModel:(ZDidRDetailModel *)didOModel {
    _didOModel = didOModel;
    _describeTextView.text = _didOModel.title;
    [_imageBtn1 sd_setBackgroundImageWithURL:[NSURL URLWithString:_didOModel.part] forState: UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Reference_picture"]];
    [_imageBtn2 sd_setBackgroundImageWithURL:[NSURL URLWithString:_didOModel.report] forState: UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Reference_picture"]];
    [_imageBtn3 sd_setBackgroundImageWithURL:[NSURL URLWithString:_didOModel.xray] forState: UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Reference_picture"]];
    
    _doctordescribeTextView.text = [_didOModel.other isEqualToString:@""]? _didOModel.content:_didOModel.other;
    _voiceTime.text = _didOModel.voice_time;
    _d_voiceTime.text = _didOModel.d_voice_time;
    if ([_didOModel.voice isEqualToString:IconURL]) {
        _voiceBtn.hidden = YES;
    }
    if ([_didOModel.d_voice isEqualToString:IconURL]) {
        _doctorvoiceBtn.hidden = YES;
    }
    if (self.ishiden == NO) {
        if ([_didOModel.is_dim isEqualToString:@"1"]) {
            [_imageBtn1.imageView addSubview:self.bar];
            [_bar mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(_imageBtn1);
            }];
            [_imageBtn2.imageView addSubview:self.bar2];
            [_bar2 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(_imageBtn2);
            }];
            [_imageBtn3.imageView addSubview:self.bar3];
            [_bar3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.equalTo(_imageBtn3);
            }];
        }
    }
    if([_didOModel.type intValue] == 1) {
        _addLabel.text = @"照片不清晰";
    }else if ([_didOModel.type intValue] ==2) {
        _addLabel.text = @"缺少照片";
    }else if ([_didOModel.type intValue] == 3) {
        _addLabel.text = @"照片上传错误";
    }else if ([_didOModel.type intValue] == 4) {
        _addLabel.text = @"其他";
    }
    _consumerView.timeLabel.text = _didOModel.time;
    if([_didOModel.other isEqualToString:@""]) {
        [_doctordescribeTextView mas_updateConstraints:^(MASConstraintMaker *make)  {
            make.height.offset(0);
        } ];
//        _doctordescribeLabel.hidden = YES;
        self.mainScrollView .contentSize = CGSizeMake(SCREEN_Width, 1.6*SCREEN_Height);
        [UIView animateWithDuration:0.1f animations:^{
            [self.view layoutIfNeeded];
        }];
    }

}

-(void)setDOModel:(ZDoctorTModel *)dOModel {
    _dOModel = dOModel;
    _doctorView.dModel = _dOModel;
}



// 跳转到支付界面
- (void)jumpPayViewController {
    ZPayViewController * ZPVc = [[ZPayViewController alloc] init];
    ZPVc.pid = self.qId;
    [self.navigationController pushViewController:ZPVc animated:YES];
}
// 跳转到分享界面
- (void)shareAction {
    if (_userInfo.uid && ![_userInfo.uid isEqualToString:@""]) {
        [YJProgressHUD showMessage:@"假装在分享" inView:self.view];
    }
}

//跳转医生主页
- (void)GoDotorDetail {
    if (!_dOModel || [_dOModel.did isEqualToString:@""]) {
        return;
    }
    ZDoctorViewController * ZDVc = [[ZDoctorViewController alloc] init];
    ZDVc.did = _dOModel.did;
    [self.navigationController pushViewController:ZDVc animated:YES];
}

// lazy
- (UIScrollView *)mainScrollView {
    if (_mainScrollView) {
        return _mainScrollView;
    }
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height)];
    _mainScrollView.contentSize = CGSizeMake(SCREEN_Width,1.8*SCREEN_Height);
    return _mainScrollView;
}

- (PointOutView *)pointOutView {
    if (_pointOutView) {
        return _pointOutView;
    }
    _pointOutView = [[PointOutView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_Width, SCREEN_Height)];
    _pointOutView.pointDeleGate  = self;
    return _pointOutView;
}


- (UIToolbar *)bar {
    if (_bar) {
        return _bar;
    }
    _bar = [[UIToolbar alloc] initWithFrame:CGRectZero];
    _bar.barStyle = UIBarStyleBlackTranslucent;
    return _bar;
    
}
- (UIToolbar *)bar2 {
    if (_bar2) {
        return _bar2;
    }
    _bar2 = [[UIToolbar alloc] initWithFrame:CGRectZero];
    _bar2.barStyle = UIBarStyleBlackTranslucent;
    return _bar2;
    
}
- (UIToolbar *)bar3 {
    if (_bar3) {
        return _bar3;
    }
    _bar3 = [[UIToolbar alloc] initWithFrame:CGRectZero];
    _bar3.barStyle = UIBarStyleBlackTranslucent;
    return _bar3;
}


@end
